package com.yhyvr.console.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lz.kit.LogKit;
import com.lz.mybatis.jdbc.auto.dao.SysReqUrlMapper;
import com.lz.mybatis.jdbc.auto.model.SysReqUrl;
import com.lz.mybatis.jdbc.auto.model.SysReqUrlExample;
import com.lz.tool.LzStringUtils;
import com.lz.tool.ToolClassSearcher;
import com.lz.tool.UUIDTool;
import com.yhyvr.console.controller.ControllerQueryTool;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.List;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Service
public class SysReqUrlService{
	@Resource
	private SysReqUrlMapper sysReqUrlMapper;
	@Resource
	private JdbcTemplate jdbcTemplate;
	public String add(){
		return "console/sysReqUrl/sysReqUrl_form";
	}

	public String edit(ModelMap modelMap,String id){
		modelMap.put("sysReqUrl", sysReqUrlMapper.selectByPrimaryKey(id));
		return "console/sysReqUrl/sysReqUrl_form";
	}

	public String list(){
		return "console/sysReqUrl/sysReqUrl_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
		if(bootGridReq.getSort()!=null) {
			page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
		}
		SysReqUrlExample sysReqUrlExample = new SysReqUrlExample();
		ControllerQueryTool.setSysReqUrlCriteria(bootGridReq.getQuery(),sysReqUrlExample.createCriteria());
		List<SysReqUrl> sysReqUrlList = sysReqUrlMapper.selectByExample(sysReqUrlExample);
		return new BootStrapGridResp(page.getTotal(),sysReqUrlList);
	}

	public HttpResult save(String formObjectJson){
		try{
			SysReqUrl sysReqUrl= JSON.parseObject(formObjectJson,SysReqUrl.class);
			String id=sysReqUrl.getId();
			if (id!=null) {
				sysReqUrlMapper.updateByPrimaryKeySelective(sysReqUrl);
				return CommonResp.getSuccess();
			}else{
				sysReqUrl.setId(UUIDTool.getUUID());
				sysReqUrlMapper.insertSelective(sysReqUrl);
				return CommonResp.getSuccess();
			}
		}catch(Exception e){
			LogKit.error(e.getMessage(),e);
			return CommonResp.getError();
		}

	}

	public  HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			sysReqUrlMapper.deleteByPrimaryKey(String.valueOf(id));
		}
		return CommonResp.getSuccess();
	}


}
